/**
 * Created by oneminute on 8/6/14.
 */
var Server = require('socket.io');
var io = new Server();
var nsp = io.of("/chat");
var lz = require("../lib/utils/modules")();

//io.path("/socket.io");
nsp.on('connection', function(_socket) {
	console.log('a new connection founded. id: ' + _socket.id);
	/*_socket.send('欢迎来到乐众聊天室。', function(_confirmed) {
        console.log('ack: ' + _confirmed);
    });*/
    _socket.on('iwant.connect', function(_data, _ack) {
		console.log('receive msg: ' + _data.uid);
        //console.log('uid: ' + lz.Utils.decrypt(_data.uid));
        if (_data.uid) {
            _ack({res: true});
        } else {
            _ack({res: false, reason: '没有提供有效的uid'});
        }

	});
    _socket.on('iwant.message', function(_data, _ack) {
        console.log('iwant.message: ' + JSON.stringify(_data));
        _ack(true);
        /*_socket.emit('iwant.message', _data, function() {
            console.log('iwant.message: 响应消息。');
        });*/
    });
    _socket.on('disconnect', function() {
        console.log('disconnect: ');
    });
    _socket.on('error', function(_err) {
        console.log('error: ' + _err);
    });
    _socket.on('message', function(_data) {
        console.log('message: ' + JSON.stringify(_data));
    });
});

io.listen(3000, function() {
	lz.Logger.info('Server listening at port 3000.');
});